from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from categoriesapp.forms import CategoryForm
from categoriesapp.models import Category
from collectionsapp.models import Collection
from uidpapp.models import UIDP
 
def create_category(request):
    if request.method == 'POST':
        form = CategoryForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            category = form.save(cd)

            return HttpResponseRedirect('/categories/%d/' % category.id)
    else:
        form = CategoryForm()
        
    return render_to_response('new_category.html', locals())
    
def list_categories(request):
    return render_to_response('categories.html', locals())

def get_category(request, category_id):
    category = Category.objects.filter(pk=category_id).order_by("name")
    return render_to_response('category.html', locals())

def list_category_uidps(request, collection_id, category_id):
    categories = Category.objects.all().order_by("name")
    collections = Collection.objects.all().order_by("name")
    
    collection = Collection.objects.get(pk=collection_id)
    category = Category.objects.get(pk=category_id)
    
    uidps = UIDP.objects.filter(collections=collection).filter(categories=category).order_by("name")
    
    return render_to_response('uidps_list.html', locals())